package com.diver.flinkdemo.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.api.R;
import com.diver.flinkdemo.entity.ResultConfig;
import com.diver.flinkdemo.readervo.TemplateDefinition;
import com.diver.flinkdemo.readervo.TerminalTemplate;
import com.diver.flinkdemo.service.IHttpService;
import com.diver.flinkdemo.service.impl.HttpServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * @author LuJW
 * @date 2023/4/27 14:07
 */
@Slf4j
public class TemplateFileReader {
    //本地，需要在本地启统一平台nacos服务
//    private static String url = "http://localhost:8001/nacos/v1/cs/configs?tenant=97a8b39a-99fa-4b8f-bee4-2e7a0ce74015&dataId=&group=DEFAULT_GROUP&appName=&config_tags=&pageNo=1&pageSize=200&search=accurate&accessToken=";
    //测试，读取校企云测试服务
    private static String url = "http://172.16.100.61:8001/nacos/v1/cs/configs?tenant=97a8b39a-99fa-4b8f-bee4-2e7a0ce74015&dataId=&group=DEFAULT_GROUP&appName=&config_tags=&pageNo=1&pageSize=200&search=accurate&accessToken=";

    private static IHttpService httpService = new HttpServiceImpl();

    /**
     * 从NACOS中读取模板
     *
     * @return
     */
    public static void loadTemplateByNACOS() {
//        if(StringUtils.isBlank(TemplateConfiguration.SERVER_ADDR)){
//            log.error("SERVER_ADDR为空，无需获取业务模块配置");
//            return;
//        }
//        configService = createConfigService();
//        if (configService == null) {
//            log.warn("loadTemplateByNACOS fail");
//        }
//        try {
//        url = url.replaceAll("serverUrl", TemplateConfiguration.SERVER_ADDR);
        ResultConfig<String> result = httpService.doGet(url, null, null, "utf-8");
        log.info("result->{}",result);
        if (result.getResultCode() != 200) {
            log.error("获取模板配置错误：{}", result.getResultCode());
            return;
        }
        JSONObject resultJson = JSONObject.parseObject(result.getResultData());
        JSONArray pageItems = resultJson.getJSONArray("pageItems");
        if (pageItems.size() == 0) {
            log.error("暂无模板配置");
            return;
        }
        for (int i = 0; i < pageItems.size(); i++) {
            JSONObject jsonObject = (JSONObject) pageItems.get(i);
            String dataId = jsonObject.getString("dataId");
            String group = jsonObject.getString("group");
            String content = jsonObject.getString("content");
            if(dataId.contains(TerminalTemplate.TERMINAL_TEMPLATE_PREFIX)){
                TerminalTemplate terminalTemplate = JSON.parseObject(content, TerminalTemplate.class);
                TerminalTemplate.terminalTemplateMap.put(dataId, JSON.parseObject(content, TerminalTemplate.class));
            }else {
                TemplateDefinition.businessTemplateMap.put(dataId, JSON.parseObject(content, TemplateDefinition.class));
            }
        }

//            String configInfo = configService.getConfig(null, TemplateConfiguration.ROUTE_GROUP, TemplateConfiguration.DEFAULT_TIMEOUT);
//            if (StringUtils.isNotBlank(configInfo)) {
//                log.info("获取网关当前配置:\r\n{}", configInfo);
//                TemplateDefinition.templateDefinition = JSON.parseObject(configInfo, TemplateDefinition.class);
//            }
//        } catch (NacosException e) {
//            log.error("初始化网关路由时发生错误", e);
//            e.printStackTrace();
//        }

//        dynamicTemplateByNACOSListener(TemplateConfiguration.TEMPLATE_ID, TemplateConfiguration.ROUTE_GROUP);
    }
}
